package com.opensky.module.sys.domain.jpa.entity;


import com.opensky.module.sys.domain.jpa.repository.SysDeptRepository;
import com.opensky.module.sys.interfaces.frm.SysDeptQueryFrm;
import com.opensky.common.infrastructure.base.jpa.BaseEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.criteria.Predicate;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

/**
 * 部门表(表sys_dept)ORM实体类
 *
 * @author 中国印
 * @date 2021-11-27 21:42:51
 */
@EqualsAndHashCode(callSuper = true)
@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@DynamicUpdate
@DynamicInsert
@Table(name = "sys_dept")
public class SysDept extends BaseEntity<SysDeptRepository, SysDept, String> {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "JDBC")
    @Column(name = "id", insertable = false, updatable = false, nullable = false, columnDefinition = "bigint(20) COMMENT '表主键'")
    private String id;
    /**
     * 上级部门ID，一级部门为0
     */
    @Column(name = "parent_id")
    private Long parentId;
    /**
     * 部门名称
     */
    @Column(name = "name")
    private String name;
    /**
     * 部门编号
     */
    @Column(name = "code")
    private String code;
    /**
     * 类型 0.公司 1.部门 2.科室/组
     */
    @Column(name = "type")
    private Integer type;
    /**
     * 排序值，越小越靠前
     */
    @Column(name = "sort")
    private Integer sort;
    /**
     * 创建人
     */
    @Column(name = "create_by")
    private Long createBy;
    /**
     * 更新人
     */
    @Column(name = "update_by")
    private Long updateBy;
    /**
     * 创建时间
     */
    @Column(name = "create_time")
    private Date createTime;
    /**
     * 更新时间
     */
    @Column(name = "update_time")
    private Date updateTime;

    private static SysDeptRepository repository() {
        return SysDept.Holder.REPO;
    }

    private static final class Holder {
        private static final SysDeptRepository REPO = getRepository(SysDeptRepository.class, SysDept.class);
    }

    /**
     * Frm转Specification查询条件
     *
     * @param frm 查询参数对象
     * @return 返回JPA所需的查询条件对象
     */
    public static Specification<SysDept> getSpecification(SysDeptQueryFrm frm) {
        if (frm.getPageNum() == null || frm.getPageNum() <= 0) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null || frm.getPageSize() <= 0) {
            frm.setPageSize(10);
        }
        return (root, query, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();
            if (!StringUtils.isEmpty(frm.getId())) {
                predicateList.add(criteriaBuilder.equal(root.get("id"), frm.getId()));
            }
            if (frm.getParentId() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("parentId"), frm.getParentId()));
            }
            if (!StringUtils.isEmpty(frm.getName())) {
                predicateList.add(criteriaBuilder.equal(root.get("name"), frm.getName()));
            }
            if (!StringUtils.isEmpty(frm.getCode())) {
                predicateList.add(criteriaBuilder.equal(root.get("code"), frm.getCode()));
            }
            if (frm.getType() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("type"), frm.getType()));
            }
            if (frm.getSort() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("sort"), frm.getSort()));
            }
            if (frm.getCreateBy() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("createBy"), frm.getCreateBy()));
            }
            if (frm.getUpdateBy() != null) {
                predicateList.add(criteriaBuilder.equal(root.get("updateBy"), frm.getUpdateBy()));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("createTime").as(String.class), frm.getCreateTimeEnd()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
                predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeBegin()));
            }
            if (!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
                predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("updateTime").as(String.class), frm.getUpdateTimeEnd()));
            }
            return criteriaBuilder.and(predicateList.toArray(new Predicate[0]));
        };
    }

}
